package com.joysuch.wwyt.dynamicdata.controller;


import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiCoreException;
import com.joysuch.wwyt.dynamicdata.entity.BaseDynamicFieldConfig;
import com.joysuch.wwyt.dynamicdata.service.BaseDynamicFieldConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jwk
 * @since 2021-11-26
 */
@Api(tags = "动态字段配置")
@Slf4j
@RestController
@RequestMapping("/data/dynamic")
public class BaseDynamicController {

    @Autowired
    private BaseDynamicFieldConfigService baseDynamicFieldConfigService;

    @PostMapping("/cfg/saveOrUpdateBatch")
    @ApiOperation(value = "保存或修改")
    @RequiresPermissions("dynamicdata:dynamic-biz:update")
    public ResultBean saveOrUpdateBatch(@RequestBody List<BaseDynamicFieldConfig> dynamicFieldConfigs) {
        try {
            baseDynamicFieldConfigService.saveConfigOrUpdateBatch(dynamicFieldConfigs);
            return ResultBean.defaultSuccessResult();
        } catch (ApiCoreException ae) {
            log.error("动态字段保存或修改失败", ae);
            return ResultBean.fail(ae.getCode(), ae.getMessage());
        }
    }

    @PostMapping("/cfg/deleteBatch")
    @ApiOperation(value = "批量删除")
    @RequiresPermissions("dynamicdata:dynamic-biz:update")
    public ResultBean delete(@RequestBody List<Long> configIds) {
        baseDynamicFieldConfigService.batchDelete(configIds);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("/cfg/list")
    @ApiOperation(value = "列表")
    public ResultBean configList(@RequestParam(value = "bizMappingId") Long bizMappingId) {
        return ResultBean.success(baseDynamicFieldConfigService.findByBizMappingId(bizMappingId));
    }

    @GetMapping("/biz/list")
    @ApiOperation(value = "所有支持业务列表")
    public ResultBean bizMappingList() {
        return ResultBean.success(baseDynamicFieldConfigService.findAllBizMapping());
    }


    @GetMapping("/summary/list")
    @ApiOperation(value = "业务列表汇总查询")
    public ResultBean summaryList() {
        return ResultBean.success(baseDynamicFieldConfigService.findSummaryList());
    }

    @GetMapping("/component/list")
    @ApiOperation(value = "组件查询")
    public ResultBean componentTypeList() {
        return ResultBean.success(baseDynamicFieldConfigService.componentTypeList());
    }


}

